<template>
  <div>
    <div class="back">
      <el-form
        class="form"
        label-position="left"
        :model="form"
        :rules="rules"
        ref="form"
      >
        <p class="title">欢迎登录后台管理系统</p>
        <el-form-item prop="shoujihao">
          <el-input
            v-model="form.shoujihao"
            @keyup.native.enter="onSubmit"
            placeholder="请输入管理员账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="mima">
          <el-input
            v-model="form.mima"
            @keyup.native.enter="onSubmit"
            type="password"
            placeholder="请输入管理员密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="onSubmit()"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import httpApi from "@/myaxios";

export default {
  data() {
    return {
      form: {
        shoujihao: "",
        mima: "",
      },
      rules: {
        shoujihao: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "请填写正确的手机号",
            trigger: "blur",
          },
        ],
        mima: [
          { required: true, message: "请填写密码", trigger: "blur" },
          {
            pattern: /^\w{6,9}$/,
            message: "6~9位字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          httpApi.BackUser.login(this.form).then((res) => {
            console.log(res);
            if (res.data.length >= 1) {
              console.log("666");
              //将登录用户的id存在localstory中
              localStorage.setItem("id", res.data[0].id);
              this.$message.success("登录成功");
              this.$router.push("/home/commodity-list");
            } else {
              this.$refs["form"].resetFields();
              this.$message.error("用户名或密码错误");
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.form {
  display: block;
  width: 20%;
  position: absolute;
  top: 30%;
  left: 40%;
}

.form .title {
  text-align: center;
  font-size: 1.5em;
  color: white;
  margin-bottom: 20px;
}

.back {
  height: 100vh;
  background-image: linear-gradient(125deg, #cf8c8c, #af94b7, #3f3d63, #99c1c6);
  background-size: 500%;
  animation: animate 20s infinite;
}

@keyframes animate {
  0% {
    background-position: 0% 50%;
  }

  50% {
    background-position: 100% 50%;
  }

  100% {
    background-position: 0% 50%;
  }
}
</style>
